﻿using System;
using System.Windows;
using Kiva7.DataRequests;
using Kiva7.Images;
using Kiva7.Pages;
using Kiva7.RatingReminders;
using Kiva7.Settings;
using Kiva7.Translations;
using Kiva7.ValueConverter;
using Kiva7.ViewModels;
using Microsoft.Phone.Controls;

namespace Kiva7
{
    public partial class MainPage
    {
        public static readonly Uri PageUrl = new Uri("/Pages/MainPage.xaml", UriKind.RelativeOrAbsolute);

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            DataContext = new FakeObject();

            CreateStandardControls();
        }

        private bool _firstLoad = true;

        private void MainPageLoaded(object sender,
                                    RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(AppSettings.Current.LenderIdSetting))
            {
                if (_firstLoad)
                {
                    _firstLoad = false;

                    DoNavigate(FirstStartPage.PageUrl);
                }
                else
                {
                    Error.Visibility = Visibility.Collapsed;
                    AccountCreation.Visibility = Visibility.Visible;
                    Pivot.Visibility = Visibility.Collapsed;
                }
            }
            else
            {
                DataContext = DataController.ViewModel;
                loansControl1.SetBindingList(DataController.ViewModel.Loans.Data);
                loansAndDetailControl1.SetBindingList(DataController.ViewModel.Loans.Data);

                CheckRatingReminder();
            }
        }

        private static void CheckRatingReminder()
        {
            RatingReminder.PerDays = 30;
            RatingReminder.PerUseCount = 5;
            RatingReminder.MessageTitle = AppResources.RateThisApplication;
            RatingReminder.MessageText = AppResources.RatingReminder;

            RatingReminder.CheckReminder();
        }

        protected override void CreateApplicationBar()
        {
            AddBarButton(AppResources.Help, ImageUrls.Help, HelpClick);
            AddBarButton(AppResources.Search, ImageUrls.Search, SearchClick);
            AddBarButton(AppResources.Map, ImageUrls.Map, ViewMapClick);
            AddBarButton(AppResources.Refresh, ImageUrls.Refresh, RefreshClick);

            //AddBarMenuItem(AppResources.Refresh, RefreshClick);
            AddBarMenuItem(AppResources.Settings, SettingsClick);
            AddBarMenuItem(AppResources.Search, SearchClick);
            AddBarMenuItem(AppResources.About, AboutClick);
        }

        private void SettingsClick(object sender,
                                    EventArgs e)
        {
            DoNavigate(SettingPage.PageUrl);
        }

        private void SearchClick(object sender,
                                  EventArgs e)
        {
            DoNavigate(SearchFilterPage.PageUrl);
        }

        private void ViewMapClick(object sender,
                                   EventArgs e)
        {
            DoNavigate(MainMapPage.PageUrl);
        }

        private void HelpClick(object sender,
                                EventArgs e)
        {
            DoNavigate(HelpPage.PageUrl);
        }

        private void AboutClick(object sender,
                                 EventArgs e)
        {
            DoNavigate(AboutPage.PageUrl);
        }


        private static void RefreshClick(object sender,
                                   EventArgs e)
        {
            DataCache.Current.Clear();
            DataController.RefreshData();
        }

        private void FirstStartPageClick(object sender,
                                  RoutedEventArgs e)
        {
            DoNavigate(FirstStartPage.PageUrl);
        }

        private void Pivot_LoadingPivotItem(object sender, PivotItemEventArgs e)
        {
            BindableApplicationBar.IsVisible = e.Item == pvLender;
        }
    }
}